-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Enhance Database Modeling and SQLAlchemy Compatibility #1593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cubxxw, thanks for your interest and efforts!
I think we don't need any of these changes
|
||
|
||
# Shared properties | ||
class UserBase(SQLModel): | ||
email: EmailStr = Field(unique=True, index=True, max_length=255) | ||
email: EmailStr = Field( | ||
unique=True, index=True, max_length=255, sa_type=String(255) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In current version of SQLModel, setting max_length
will already create correct column type email VARCHAR(255)
@@ -18,20 +21,20 @@ class UserCreate(UserBase): | |||
|
|||
|
|||
class UserRegister(SQLModel): | |||
email: EmailStr = Field(max_length=255) | |||
email: str = Field(max_length=255) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a point in this change. Moreover, it will allow creating users with invalid emails
# Remove foreign_key constraint, but keep it indexed for performance | ||
owner_id: uuid.UUID = Field(index=True, nullable=False) | ||
owner: User | None = Relationship( | ||
back_populates="items", | ||
sa_relationship_kwargs={ | ||
"primaryjoin": "User.id == Item.owner_id", | ||
"foreign_keys": "[Item.owner_id]", | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What kind of performance boost will this give?
I think we don't need it
This pull request makes several changes to the
backend/app/models.py
file to enhance database modeling and improve compatibility with SQLAlchemy. The key updates include adjustments to field definitions, changes to relationships, and the removal of certain constraints.Changes to field definitions:
email
field inUserBase
to explicitly usesa_type=String(255)
for better SQLAlchemy compatibility.email
field type inUserRegister
,UserUpdate
, andUserUpdateMe
fromEmailStr
tostr
for consistency and to avoid type-related issues.Changes to relationships:
items
relationship in theUser
model to includesa_relationship_kwargs
for defining cascading behavior and specifying join conditions.owner_id
field in theItem
model to remove theforeign_key
constraint while keeping it indexed for performance. Addedsa_relationship_kwargs
to theowner
relationship for explicit join conditions.